package tlcn.business;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Locale;
import tlcn.app.ListDishesActivity;
import tlcn.app.R;
import tlcn.data.DateMenu;
import android.R.bool;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.TextView;



public class ListDateMenuAdapter extends BaseExpandableListAdapter{

	Activity context ;
	ArrayList<DateMenu> dateMenus;
	ArrayList<Date> calendar;
	ManagerData managerData;
	SimpleDateFormat sdf= new SimpleDateFormat("dd/MM/yyyy",Locale.getDefault());
	boolean flagSang=true,flagTrua=true,flagToi=true;
	public ListDateMenuAdapter(Activity context,ArrayList<DateMenu> dateMenus) {
		this.context=context;
		this.dateMenus=dateMenus;
		calendar=initListDate();
		managerData= new ManagerData(context);
	}
	
	private ArrayList<Date> initListDate(){
		ArrayList<Date> list = new ArrayList<Date>();
		Calendar ca= Calendar.getInstance();
		String s=sdf.format(ca.getTime());
		String strArrtmp[]=s.split("/");
		int da=Integer.parseInt(strArrtmp[0])-1;
		int mo=Integer.parseInt(strArrtmp[1]);
		int ye=Integer.parseInt(strArrtmp[2]);
		int max=30;
		if(mo==1||mo==3||mo==5||mo==7||mo==8||mo==10||mo==12)
			max=31;
		else if (mo==2){
			if ((ye%4==0&&ye%100!=0)||(ye%400==0))
				max=29;
			else max=28;
		}
		for (int i=0;i<14;i++){
			if (da==max)	{
				da=1;
				if(mo==12){mo=1;ye++;}
				else mo++;
			}else da++;
			list.add(Date.valueOf(ye+"-"+mo+"-"+da));
		}
		return list;
	}
	
	@Override
	public Object getChild(int groupPosition, int childPosititon) {
		return managerData.getDishofDate(calendar.get(groupPosition).toString()).get(childPosititon);
	}

	@Override
	public long getChildId(int groupPosition, int childPosititon) {
		return childPosititon;
	}

	@Override
	public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView,
			ViewGroup parent) {
		
		if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this.context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.item_child, null);
        }
		Button btnXoa=(Button) convertView.findViewById(R.id.btnXoa);
		TextView txtChild = (TextView) convertView.findViewById(R.id.lblChild);
		TextView txtMeal = (TextView) convertView.findViewById(R.id.lblMeal);
		if (!isLastChild)
		{
			btnXoa.setText(R.string.set_1);
			btnXoa.setTag("delete");
			final DateMenu childDate= (DateMenu)getChild(groupPosition, childPosition);
			if((flagSang&&childDate.getMeal().equals("Bữa sáng"))
					||(flagTrua&&childDate.getMeal().equals("Bữa trưa"))
					||(flagToi&&childDate.getMeal().equals("Bữa tối")))
				txtMeal.setText(childDate.getMeal()+": ");
			if (childDate.getMeal().equals("Bữa sáng"))
				flagSang=false;
			else if (childDate.getMeal().equals("Bữa trưa"))
				flagTrua=false;
			else flagToi=false;
			txtChild.setText(childDate.getDish().getDishName());
		}
		else 
		{
			txtChild.setText("");
			btnXoa.setText(R.string.set_2);
			btnXoa.setTag("add");
		}
        btnXoa.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				//Remove
				if(v.getTag().toString()=="delete"){
					AlertDialog.Builder builder = new AlertDialog.Builder(context);
					final DateMenu childDate= (DateMenu)getChild(groupPosition, childPosition);
	                builder.setMessage("Bạn có muốn xóa món "+childDate.getDish().getDishName()+
	                		" ra khỏi bữa "+childDate.getMeal()+" ngày "+sdf.format(childDate.getDate()).toString());
	                builder.setCancelable(false);
	                builder.setPositiveButton("Có",
	                        new DialogInterface.OnClickListener() {
	                            public void onClick(DialogInterface dialog, int id) {
	                            	managerData.removeDishOfDate(childDate.getDate().toString(), childDate.getDish().getDishID(), childDate.getMeal());
	                				notifyDataSetChanged();
	                            }
	                        });
	                builder.setNegativeButton("Không",
	                        new DialogInterface.OnClickListener() {
	                            public void onClick(DialogInterface dialog, int id) {
	                                dialog.cancel();
	                            }
	                        });
	                AlertDialog alertDialog = builder.create();
	                alertDialog.show();
				}
				else{
				//Add
					Intent intent= new Intent(context,ListDishesActivity.class);
					intent.putExtra("date",sdf.format(calendar.get(groupPosition)).toString());
					context.startActivity(intent);
				}
			}
		});
		return convertView;
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		return managerData.getDishofDate(calendar.get(groupPosition).toString()).size()+1;
	}

	@SuppressLint("SimpleDateFormat")
	@Override
	public Object getGroup(int groupPosition) {
		return sdf.format(calendar.get(groupPosition)).toString();
	}

	@Override
	public int getGroupCount() {
		return calendar.size();
	}

	@Override
	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup arg3) {
		String headerTitle = (String) getGroup(groupPosition);
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this.context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.item_group,null);
        }
        TextView lblListHeader = (TextView) convertView
                .findViewById(R.id.lblListHeader);
        lblListHeader.setTypeface(null, Typeface.BOLD);
        lblListHeader.setText(headerTitle);
        flagSang=true;flagTrua=true;flagToi=true;
		return convertView;
	}

	@Override
	public boolean hasStableIds() {
		return false;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}

	
}
